package logic

import (
	"context"

	"github.com/comfygpt-backend-go/common/utils"
	"github.com/comfygpt-backend-go/service/component-rpc/component"
	"github.com/comfygpt-backend-go/service/component-rpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetComponentByIdLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetComponentByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetComponentByIdLogic {
	return &GetComponentByIdLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GetComponentByIdLogic) GetComponentById(in *component.ComponentIdRequest) (*component.ComponentItem, error) {
	res, err := l.svcCtx.ComponModel.FindOne(l.ctx, in.Id)
	if err != nil {
		return nil, err
	}

	return &component.ComponentItem{
		Id:          res.Id,
		Name:        res.Name,
		Description: res.Description,
		Category:    res.Category,
		Icon:        res.Icon,
		Tags:        utils.StringToArray(res.Tags),
	}, nil
}
